/*
 * ! ******************************************************************************
 *
 * Pentaho Data Integration
 *
 * Copyright (C) 2002-2017 by Hitachi Vantara : http://www.pentaho.com
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ******************************************************************************/

package org.pentaho.di.ui.core.dialog;

import java.io.File;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.*;
import org.pentaho.di.core.Const;
import org.pentaho.di.core.util.ApplicationPathUtil;
import org.pentaho.di.ui.core.PropsUI;
import org.pentaho.di.ui.core.database.dialog.DatabaseDialog;
import org.pentaho.di.ui.core.gui.GUIResource;
import org.pentaho.di.ui.dbdto.DbdriverDto;
import org.pentaho.di.ui.trans.step.BaseStepDialog;
import org.yzbdl.lanius.core.database.dialog.DataOverrideHandler;

import cn.hutool.core.date.DateTime;

/**
 * Displays an HTML page.
 *
 * @author Matt
 * @since 22-12-2005
 */
public class ShowDbBrowserDialog extends Dialog {
    private static Class<?> PKG = DatabaseDialog.class; // for i18n purposes, needed by Translator2!!

    private String dialogTitle;
    private String url;
    private String content;

    private Button wOK;
    private FormData fdOK;
    private Listener lsOK;
    private FormData fdFields;

    private Table table;
    private FormData fdBrowser;

    private Shell shell;
    private PropsUI props;

    private int buttonHeight = 30;

    private DataOverrideHandler handler;

    public ShowDbBrowserDialog(Shell parent, String dialogTitle, String url, DataOverrideHandler handler) {
        super(parent, SWT.NONE);
        props = PropsUI.getInstance();
        this.dialogTitle = dialogTitle;
        this.url = url;
        this.handler = handler;
    }

    public void open(DbdriverDto dbdriverDto) {
        Shell parent = getParent();
        Display display = parent.getDisplay();

        shell = new Shell(parent, SWT.APPLICATION_MODAL | SWT.MIN);
        shell.setImage(GUIResource.getInstance().getImageSpoon());
        props.setLook(shell);

        FormLayout formLayout = new FormLayout();
        formLayout.marginWidth = Const.FORM_MARGIN;
        formLayout.marginHeight = Const.FORM_MARGIN;

        shell.setLayout(formLayout);
        shell.setText(dialogTitle);

        Table table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);

        table.setHeaderVisible(true);
        TableColumn versionColumn = new TableColumn(table, SWT.NONE);
        versionColumn.setText("版本");
        versionColumn.setWidth(200);
        TableColumn pushDate = new TableColumn(table, SWT.NONE);
        pushDate.setText("发布时间");
        pushDate.setWidth(200);
        TableColumn size = new TableColumn(table, SWT.NONE);
        size.setText("大小");
        size.setWidth(200);
        TableColumn operating = new TableColumn(table, SWT.NONE);
        operating.setText("操作");
        operating.setWidth(220);
        // 表格渲染数据
        List<DbdriverDto.DataBean> data = dbdriverDto.getData();
        // 获取本地已经下载的驱动
        File driverList = new File(ApplicationPathUtil.getPath() + "/lib");
        String[] list = driverList.list();
        for (int i = 0; i < data.size(); i++) {
            TableItem item = new TableItem(table, SWT.NONE);
            item.setText(0, data.get(i).getDriverVersion());
            item.setText(1, String.valueOf(new DateTime(data.get(i).publishTime)));
            item.setText(2, String.valueOf((Long.valueOf(data.get(i).getSize()) / 1024)) + "KB");
            Image image = GUIResource.getInstance().getImage("/ui/images/place_holder.png", 1, 40);
            item.setImage(3, image);
            TableEditor te = new TableEditor(table);
            Button btn = new Button(table, SWT.NONE);
            btn.setText("下载");
            for (int j = 0; j < list.length; j++) {
                if (data.get(i).getDriverPackage().equals(list[j])) {
                    btn.setEnabled(false);
                    continue;
                }
            }

            te.grabHorizontal = false;
            te.grabVertical = false;

            te.horizontalAlignment = SWT.CENTER;
            te.verticalAlignment = SWT.CENTER;

            te.minimumHeight = 25;
            te.minimumWidth = 80;

            te.setEditor(btn, item, 3);
        }
        table.setLinesVisible(true);

        // Composite
        Composite buttonsPane = new Composite(shell, SWT.NONE);
        Color grey = new Color(display, 240, 240, 240);
        buttonsPane.setBackground(grey);
        buttonsPane.setLayout(new FormLayout());
        FormData fdButtonsPane = new FormData();
        fdButtonsPane.left = new FormAttachment(0, 0);
        fdButtonsPane.right = new FormAttachment(100, 0);
        fdButtonsPane.bottom = new FormAttachment(100, 0);
        buttonsPane.setLayoutData(fdButtonsPane);
        FormData tableFormData = new FormData(800, 600);
        table.setLayoutData(tableFormData);

        // Detect [X] or ALT-F4 or something that kills this window...
        shell.addShellListener(new ShellAdapter() {
            public void shellClosed(ShellEvent e) {
                ok();
            }
        });

        BaseStepDialog.setSize(shell, 800, 600, true);

        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    public void dispose() {
        shell.dispose();
    }

    private void ok() {
        dispose();
        // 刷新数据库连接页面
        if (handler != null) {
            handler.refreshOptions();
        }
    }
}
